package com.yuhanginfo.modules.web.web;

import java.math.BigDecimal;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yuhanginfo.common.web.BaseController;
import com.yuhanginfo.modules.web.util.WebUserUtils;
import com.yuhanginfo.modules.web.entity.PayData;
import com.yuhanginfo.modules.web.entity.WebPayHistory;
import com.yuhanginfo.modules.web.entity.PayResultData;
import com.yuhanginfo.modules.web.entity.WebCredit;
import com.yuhanginfo.modules.web.entity.WebOrders;
import com.yuhanginfo.modules.web.entity.WebTransactionHistory;
import com.yuhanginfo.modules.web.entity.WebYongUser;
import com.yuhanginfo.modules.web.service.OrderService;
import com.yuhanginfo.modules.web.service.WebPayHistoryService;
import com.yuhanginfo.modules.web.service.WebClassService;
import com.yuhanginfo.modules.web.service.WebCreditService;
import com.yuhanginfo.modules.web.service.WebTransactionHistoryService;
import com.yuhanginfo.modules.web.service.WebYongUserService;
import com.yuhanginfo.modules.web.util.PayUtils;

/**
 * 支付管理
 *
 */
@Controller
@RequestMapping(value = "${frontPath}/payment")
public class PayController extends BaseController {

	@Autowired
	private OrderService orderService;
	@Autowired
	private WebClassService webClassService;
	@Autowired
	private WebPayHistoryService payHistoryService;
	@Autowired
	private WebCreditService webCreditService;
	@Autowired
	private WebTransactionHistoryService webTransactionHistoryService;
	@Autowired
	private WebYongUserService webYongUserService;
	
	/**
	 * 接收支付结果通知
	 */
	@RequestMapping(value = "receivePayResult")
	public void receivePayResult(PayResultData payResultData, Model model){
		WebOrders webOrders = orderService.get(payResultData.getOrder_id());
		if("11".equals(payResultData.getOrder_st()) && "0000".equals(payResultData.getOrder_pay_code())){
			webOrders.setState("2");
			webOrders.setPayNo(payResultData.getFy_ssn());
			orderService.save(webOrders);
		}
	}
	
	/**
	 * 跳转支付成功页面
	 */
	@RequestMapping(value = "paySuccess")
	public String paySuccess(PayResultData payResultData, Model model){
		WebYongUser yongUser = WebUserUtils.getYongUser();
		WebOrders webOrders = orderService.selectWebOrdersByCode(payResultData.getOrder_id());
		WebPayHistory payHistory = new WebPayHistory();
		if(yongUser.getId()!=null){
			payHistory.setCustomerId(Integer.parseInt(yongUser.getId()));
		}
		payHistory.setPayResult(1);
		payHistory.setPayCode(payResultData.getOrder_id());
		payHistory.setFySsn(payResultData.getFy_ssn());
		payHistory.setMchntCd(payResultData.getMchnt_cd());
		String order_amt = payResultData.getOrder_amt();
		BigDecimal money = new BigDecimal(order_amt);
		money = money.divide(new BigDecimal("100"));
		payHistory.setOrderAmt(money.toString());
		payHistory.setOrderPayCode(payResultData.getOrder_pay_code());
		payHistory.setOrderPayMsg(payResultData.getOrder_pay_error());
		payHistory.setOrderSt(payResultData.getOrder_st());
		payHistory.setOrderDate(payResultData.getOrder_date());
		payHistory.setOrderId(payResultData.getOrder_id());
		payHistory.setPayType(payResultData.getPayType());
		payHistory.setRSA(payResultData.getMd5());
		model.addAttribute("order",webOrders);
		model.addAttribute("allClassesList",webClassService.getAllClasses());
		payHistoryService.save(payHistory);
		if("0000".equals(payResultData.getOrder_pay_code())){
			webOrders.setState("2");
			webOrders.setPayType("1");
			webOrders.setPayNo(payResultData.getFy_ssn());
			orderService.save(webOrders);
			model.addAttribute("allClassesList",webClassService.getAllClasses());
			return "modules/web/payment/paySuccess";
		}else{
			model.addAttribute("errorMessage",payResultData.getOrder_pay_error()+"，错误码:"+payResultData.getOrder_pay_code());
			return "modules/web/payment/payError";
			
		}
	}
	
	/**
	 * 跳转用户支付
	 */
	@RequestMapping(value = "goPaymentShow")
	public String goPaymentShow(String orderCode, HttpServletRequest request, HttpServletResponse response, Model model) {
		WebYongUser yongUser = WebUserUtils.getYongUser();
		if(yongUser.getId()==null){
			return "modules/web/webLogin";//到登陆页面
		}
		model.addAttribute("allClassesList",webClassService.getAllClasses());
		model.addAttribute("order", orderService.selectWebOrdersByCode(orderCode));
		if("2".equals(yongUser.getUserType())){
			model.addAttribute("credit", webCreditService.selectByUserId(Integer.parseInt(yongUser.getId())));
			return "modules/web/payment/vipPayment";
		}else{
			return "modules/web/payment/commonPayment";
		}
	}
	
	/**
	 * 跳转普通用户支付
	 */
	@RequestMapping(value = "goPayment")
	public String goPayment(String orderCode, HttpServletRequest request, HttpServletResponse response, Model model) {
		WebYongUser yongUser = WebUserUtils.getYongUser();
		if(yongUser.getId()==null){
			return "modules/web/webLogin";//到登陆页面
		}
		WebOrders order = orderService.selectWebOrdersByCode(orderCode);
		PayData payData = PayUtils.goPay(order);
		model.addAttribute("payData", payData);
		return "modules/web/payment/sendNew";
	}
	
	/**
	 * 会员确认支付
	 */
	@RequestMapping(value = "vipConfirmPayment")
	public String vipConfirmPayment(String orderCode, HttpServletRequest request, HttpServletResponse response, Model model) {
		WebYongUser yongUser = WebUserUtils.getYongUser();
		if(WebUserUtils.getYongUser().getId()==null){
			return "modules/web/webLogin";//到登陆页面
		}
		WebOrders order = orderService.selectWebOrdersByCode(orderCode);
		order.setState("2");
		order.setPayType("0");
		orderService.save(order);
		
		WebCredit credit = webCreditService.selectByUserId(Integer.parseInt(yongUser.getId()));
		credit.setUseMoney(credit.getUseMoney().add(order.getPayMoney()));
		webCreditService.save(credit);
		
		WebTransactionHistory history = new WebTransactionHistory();
		history.setUserId(yongUser.getId());
		history.setType("1");
		history.setCode(order.getCode());
		history.setMoney(order.getPayMoney());
		history.setDelFlag("0");
		history.setPayType(0);
		webTransactionHistoryService.save(history);
		
		model.addAttribute("order", order);
		model.addAttribute("allClassesList",webClassService.getAllClasses());
		return "modules/web/payment/paySuccess";
	}
	

	@RequestMapping(value = "checkPwd")
	@ResponseBody
	public boolean checkPwd(String oldPassword ,HttpServletRequest request, HttpServletResponse response, Model model) {
		WebYongUser yongUser = WebUserUtils.getYongUser();
		return webYongUserService.validatePassword(oldPassword, yongUser.getPassword());
	}
}
